package com.learn.jdk.optionDemo;

import java.util.Optional;

/**
 * Copyright: Copyright (c) 2020
 *
 * @ClassName: com.learn.jdk.optionDemo.OptionTest1
 * @Description: 该类的功能描述
 * @version: v1.0.0
 * @author: lipan
 * @date: 2020/9/3 10:09
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2020/9/3      lipan          v1.0.0               修改原因
 */

public class OptionTest1 {



    public static void main(String[] args) {

        User user=new User("aa");
        City city = new City();
        city.setName("上海");
        Provice provice = new Provice();
        provice.setCity(city);
        Address address = new Address();
        address.setProvice(provice);
        user.setAddress(address);


        //对应A对象一个熟悉B。B里面又有C ... 这种使用Option去获取某个属性要比使用if更加方便优雅

//        Optional.ofNullable(user).map(u -> u.getName()).orElse("");


        //避免get
         Optional.ofNullable(user).map(User::getAddress).map(Address::getProvice).map(Provice::getCity).map(City::getName).orElse("");

//        System.out.println(result);

        Optional<Address> address2 = Optional.ofNullable(user).map(User::getAddress);
        Optional<Address> address1 = Optional.ofNullable(user).flatMap(u -> Optional.of(user.getAddress()));

//        System.out.println(aa);

    }

    public void test() throws Exception{
        User user=new User("aa");



    }

}

class  User{



    private  Address address;



    private  String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

class Address{


    private Provice provice;

    public Provice getProvice() {
        return provice;
    }

    public void setProvice(Provice provice) {
        this.provice = provice;
    }
}


class Provice{


    private City city;

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }
}


class City{

    private  String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}